﻿namespace CatClient.Fusing
{
    using CatClient;
    using CatClient.Configuration;
    using CatClient.Util;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal class ExFuseStatistics : FuseStatistics
    {
        protected override bool IncValueProcess(object value)
        {
            if (!(value is Exception))
            {
                LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Warn($"{value.GetType().Name} isn't exception instance, could not increment with {"ExFuseStatistics"}");
                return false;
            }
            CatException exception = value as CatException;
            if (exception == null)
            {
                LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Warn($"{value.GetType().Name} not instance of {"CatException"}, could not increment with {"ExFuseStatistics"}");
                return false;
            }
            return !exception.IgnoreFuse;
        }

        protected override bool RecoveryCheck(long minute)
        {
            if (base.Statistics.Keys.Count != 0)
            {
                long num = ((IEnumerable<long>) base.Statistics.Keys).Max();
                return ((minute - num) >= this.TryInterval);
            }
            LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Warn("Exception fuse statistics check, statistics empty!");
            return true;
        }

        private int TryInterval =>
            CatConfigManager.GetConfigObject<int>("CatExTryInterval", 5);

        public override FuseType Type =>
            FuseType.Exception;

        public override int Duration =>
            CatConfigManager.GetConfigObject<int>("CatExCheckDuration", 5);

        public override long Threshold =>
            ((long) CatConfigManager.GetConfigObject<int>("CatExFuseThreshold", 50));
    }
}

